import threading
import time

class HtmlSpider(threading.Thread):
    def __init__(self,url,callback):
        super().__init__()
        self.url = url
        self.callback = callback
    def run(self) -> None:
        time.sleep(2)
        print(f"got {self.url} html text success")
        if self.callback:
            self.callback()


def main():
    sem = threading.Semaphore(3)
    def callback():
        sem.release()
    for i in range(20):
        url = f"https://www.baidu.com/{i}.html"
        sem.acquire()
        t = HtmlSpider(url,callback)
        t.start()

if __name__ == '__main__':
    main()